package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.OffWorkEntity;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;

/**
 * Created by lijiateng on 2018/8/23.
 */

public class ADA_OffWork extends CommonAdapter<OffWorkEntity> {

    private OffWorkEntity mPreItem = null;

    public ADA_OffWork(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_off_work;
    }

    @Override
    protected void convert(ViewHolder holder, OffWorkEntity offWorkEntity, int position) {
        // 设置点击事件监听
        setItemClickEvent(position, holder, offWorkEntity);

        // 设置条目
        setViews(holder, offWorkEntity);
    }

    private void setViews(ViewHolder holder, OffWorkEntity offWorkEntity) {
        // 姓名
        if (StringUtils.isTrue(offWorkEntity.getIsOnWork())) {
            holder.setSpanText(R.id.tv_staff_name, StringSpanUtils.init().addStr(offWorkEntity.getUserName()).addStr("    ")
                    .addRoundRectLabelWithRadius("在班", (TextView) holder.getView(R.id.tv_staff_name),
                            mContext.getResources().getColor(R.color.white_color),
                            mContext.getResources().getColor(R.color.order_status_reserve),
                            1, 1, 1, 0, true).build());
        } else {
            holder.setText(R.id.tv_staff_name, offWorkEntity.getUserName());
        }

        // 职责  暂时写死
//        holder.setText(R.id.tv_staff_duty, offWorkEntity.getDuty());

        // 金额
        holder.setText(R.id.tv_amount, "￥" + StringUtils.getFormattedMoney(offWorkEntity.getActualAmount()));

        // 溢缺金额
        holder.setText(R.id.tv_difference_amount, "￥" + StringUtils.getFormattedMoney(offWorkEntity.getOverAndShort()));

        // 在班时间
//        String startTime = AppDateUtil.getTimeStamp(Long.parseLong(offWorkEntity.getStartTime()), AppDateUtil.MM_DD_HH_MM1);
//        String endTime = AppDateUtil.getTimeStamp(Long.parseLong(offWorkEntity.getEndTime()), AppDateUtil.MM_DD_HH_MM1);
        holder.setText(R.id.tv_on_work_time, offWorkEntity.getStartTime() + " - " + offWorkEntity.getEndTime());
    }

    /**
     * 设置条目点击效果
     *
     * @param holder
     * @param item
     */
    private void setItemClickEvent(final int position, final ViewHolder holder, final OffWorkEntity item) {

        if (mPreItem == null) {

            if (mListener != null) {
                mListener.onItemSelected(mDatas.get(0));
            }

            holder.setBackgroundColor(R.id.ll_item_layout, mContext.getResources().getColor(R.color.white_color));

            mPreItem = mDatas.get(0);
            mPreItem.setChecked(true);
//            notifyDataSetChanged();

            return;
        }

        holder.setBackgroundColor(R.id.ll_item_layout,
                item.isChecked() ? mContext.getResources().getColor(R.color.white_color) : Color.TRANSPARENT);

        holder.setOnClickListener(R.id.ll_item_layout, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//                if (mPreItem == item) return;

                mPreItem.setChecked(false);
                item.setChecked(true);
                mPreItem = item;

                if (mListener != null) {
                    mListener.onItemSelected(item);
                }

                notifyDataSetChanged();

            }
        });
    }

    /**
     * 清空选中信息
     */
    public void clearCheck() {
        if (mPreItem == null) return;
        mPreItem.setChecked(false);
        mPreItem = null;
    }

    public interface OnItemClickListener {
        void onItemSelected(OffWorkEntity offWorkEntity);
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener l) {
        this.mListener = l;
    }

}
